package cn.yhbk.web.modules.manager.Service.Impl;

import cn.yhbk.web.common.Util.MinioUtils;
import cn.yhbk.web.common.exception.ApiException;
import cn.yhbk.web.config.MinioConfig;
import cn.yhbk.web.modules.manager.Pojo.Entity.TbGpaDetailsResource;
import cn.yhbk.web.modules.manager.Mapper.TbGpaDetailsResourceMapper;
import cn.yhbk.web.modules.manager.Pojo.VO.Resource.ResourceResp;
import cn.yhbk.web.modules.manager.Service.ITbGpaDetailsResourceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 有何不可
 * @since 2023-08-05
 */
@Service
public class TbGpaCheckDetailsServiceImpl extends ServiceImpl<TbGpaDetailsResourceMapper, TbGpaDetailsResource> implements ITbGpaDetailsResourceService {

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private MinioConfig minioConfig;

    @Override
    public List<TbGpaDetailsResource> getResourceByDetailsId(List<Long> detailIds) {
        if(detailIds.isEmpty())return new ArrayList<>();

        QueryWrapper<TbGpaDetailsResource> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TbGpaDetailsResource> lambda = wrapper.lambda();

        lambda.in(TbGpaDetailsResource::getGpaDetailId,detailIds);

        return list(lambda);
    }

    @Override
    public List<ResourceResp> getPath(List<TbGpaDetailsResource> resources) {

        List<ResourceResp> resList = new ArrayList<>();

        for(int i=0;i<resources.size();i++){
            ResourceResp resourceResp = new ResourceResp();
            TbGpaDetailsResource resource = resources.get(i);
            BeanUtils.copyProperties(resource,resourceResp);
            String fileName = resource.getPath();
            if(fileName.startsWith("http://") ||fileName.startsWith("https://") ){
                resourceResp.setUrl(fileName);
            }else{
                String path = minioUtils.getPresignedObjectUrl(minioConfig.getBucketName(), fileName);
                resourceResp.setUrl(path);
            }

            resList.add(resourceResp);
        }

        return resList;
    }


    @Override
    public boolean delResourceByDetailsId(List<Long> detailsId) {
        if(detailsId.isEmpty())return true;
        QueryWrapper<TbGpaDetailsResource> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TbGpaDetailsResource> lambda = wrapper.lambda();

        lambda.in(TbGpaDetailsResource::getGpaDetailId,detailsId);
        return remove(lambda);
    }

    @Override
    @Transactional
    public boolean updateFile(Long detailId, List<String> path) {
        if(Objects.isNull(detailId))return false;
        QueryWrapper<TbGpaDetailsResource> wrapper = new QueryWrapper<>();
        LambdaQueryWrapper<TbGpaDetailsResource> lambda = wrapper.lambda();

        lambda.eq(TbGpaDetailsResource::getGpaDetailId,detailId);
        boolean remove = remove(lambda);
        if(!remove)return false;

        List<TbGpaDetailsResource> resources = path.stream().map(item -> {
            TbGpaDetailsResource resource = new TbGpaDetailsResource();
            resource.setGpaDetailId(detailId);
            resource.setPath(item);
            return resource;
        }).collect(Collectors.toList());
        boolean saveBatch = saveBatch(resources);
        if(!saveBatch)throw new ApiException("更新失败！");
        return true;
    }
}
